(function () {

  /**
   * mode 1 新增, 2 编辑
   */
  function getFormBody(mode = '1')
  {
    return [
      {
        "type": "input-image",
        "name": "image",
        "size": "lg",
        "description": "上传尺寸：280x170px, 只能上传jpg、png，文件大小不超过2M",
        "label": "封面图",
        "accept": ".jpg,.png",
        "receiver": "post:/b/site/upload?type=image",
        "required": true
      },
      {
        "type": "text",
        "size": "lg",
        "name": "name",
        "label": "课程名称",
        "required": true
      },
      {
        "label": "课程分类",
        "type": "select",
        "size": "lg",
        "name": "course_category_id",
        "searchable": true,
        "clearable": true,
        "required": true,
        "source": "get:/b/courseC/options?state=1",
      },
      {
        "type": "radios",
        "name": "type",
        "size": "lg",
        "label": '课程类型',
        "required": true,
        "options": [
          {
            "label": "直播课程",
            "value": 1
          },
          {
            "label": "点播课程",
            "value": 2
          }
        ]
      },
      {
        "type": "input-number",
        "label": "金额(元)",
        "size": "lg",
        "name": "amount",
        "required": true,
        "validateOnChange": true,
        "validations": {
          "minimum": 0,
          "maximum": 9999999999,
        },
      },
      {
        "type": "input-number",
        "label": "过期天数(0永不过期)",
        "size": "lg",
        "name": "expired_days",
        "validateOnChange": true,
        "validations": {
          "minimum": 0,
          "maximum": 4294967295,
        },
      },
      {
        "type": "rich-text",
        "label": "课程介绍",
        "name": "content",
        "receiver": "post:/b/site/upload?type=image",
        "options": {
          "height": 500,
          // ...editorOptions,
          "menubar": false,
          "branding": false,
          "convert_urls": false,
          "toolbar_mode": 'wrap',
          "plugins": [
            "advlist autolink lists link image charmap print preview anchor",
            "searchreplace visualblocks code fullscreen",
            "insertdatetime media table paste code help wordcount"
          ],
          "toolbar": 'code undo redo restoredraft | cut copy paste pastetext | forecolor backcolor bold italic underline strikethrough link anchor | alignleft aligncenter alignright alignjustify outdent indent | styleselect formatselect fontselect fontsizeselect | bullist numlist | blockquote subscript superscript removeformat | table image media charmap emoticons hr pagebreak insertdatetime print preview | fullscreen | bdmap indent2em lineheight formatpainter',
        },
      },
    ];
  }

  function getChapterFormBody(mode = '1') {
    return [
      {
        "type": "fieldSet",
        "title": "基本信息",
        "headingClassName": "bg-gray-100",
        "body": [
          {
            "type": "hidden",
            "name": "course_id"
          },
          {
            "type": "input-text",
            "size": "lg",
            "name": "name",
            "label": "节名称",
            "required": true
          },
        ]
      }
    ]
  }

  function getSectionFormBody(mode = '1') {
    return [
      {
        "type": "fieldSet",
        "title": "基本信息",
        "headingClassName": "bg-gray-100",
        "body": [
          {
            "type": "hidden",
            "name": "course_id"
          },
          {
            "label": "章",
            "type": "select",
            "size": "lg",
            "name": "course_chapter_id",
            "searchable": true,
            "clearable": true,
            "required": true,
            "source": "get:/b/chapter/options?state=1&course_id=$course_id",
          },
          {
            "type": "input-text",
            "size": "lg",
            "name": "name",
            "label": "节名称",
            "required": true
          },
          {
            "label": "视频",
            "type": "select",
            "size": "lg",
            "name": "video_id",
            "searchable": true,
            "clearable": true,
            "required": true,
            "source": "get:/b/video/options?state=1",
          },
        ]
      }
    ]
  }

  return {
    "type": "page",
    "initApi": "/b/permits/init",
    "title": "课程分类",
    "toolbar": [
      {
        "type": "button",
        "actionType": "drawer",
        "label": "新增",
        // "visibleOn": "isInArray(this.front_end, 'scale_category_save')",
        "icon": "fa fa-plus pull-left",
        "primary": true,
        "drawer": {
          "title": "新增",
          "closeOnOutside": true,
          "closeOnEsc": true,
          "resizable": true,
          "position": "right",
          "size": "lg",
          "body": {
            "type": "form",
            // "debug": true,
            "persistData": "course_insert",
            "clearPersistDataAfterSubmit": true,
            "name": "新增表单",
            "api": "post:/b/course/save",
            "controls": getFormBody(),
          }
        }
      }
    ],
    "body": {
      "type": "crud",
      "className": "break-all",
      "hideQuickSaveBtn": true,
      "api": "get:/b/course/index",
      "quickSaveApi": "post:/b/course/batch",
      "quickSaveItemApi": "post:/b/course/item?id=$id",
      // "itemCheckableOn": "this.is_super !== 1 && this.leader_count === 0",
      "perPage": 10,
      "keepItemSelectionOnPageChange": true,
      "maxKeepItemSelectionLength": 20,
      "pageField": "page",
      "perPageField": "page_size",
      "labelTpl": "${name}",
      "syncLocation": false,
      "filter": {
        "title": "条件搜索",
        "submitText": "搜索",
        "controls": [
          {
            "label": "课程名称：",
            "type": "text",
            "size": "sm",
            "name": "name",
            "placeholder": "请输入"
          },
          {
            "label": "课程类型",
            "type": "select",
            "size": "sm",
            "name": "type",
            "searchable": true,
            "clearable": true,
            "source": "get:/b/course/type",
          },
          {
            "label": "课程分类",
            "type": "select",
            "size": "sm",
            "name": "course_category_id",
            "searchable": true,
            "clearable": true,
            "source": "get:/b/courseC/options?state=1",
          },
        ],
        "actions": [
          {
            "type": "button",
            "actionType": "clear",
            "label": "重置"
          },
          {
            "type": "button",
            "actionType": "submit",
            "label": "搜索",
            "level": "primary",
          }
        ]
      },
      "bulkActions": [
        // {
        //   "label": "批量删除",
        //   "visibleOn": "isInArray(this.front_end, 'admin_list_delete')",
        //   "level": "danger",
        //   "actionType": "ajax",
        //   "api": "post:/b/admin/del/${ids|raw}",
        //   "confirmText": "确定要批量删除?"
        // }
      ],
      "filterTogglable": true,
      "headerToolbar": [
        "filter-toggler",
        "bulkActions",
        {
          "type": "columns-toggler",
          "align": "right"
        },
        {
          "type": "pagination",
          "align": "right"
        }
      ],
      "footerToolbar": [
        "statistics",
        "switch-per-page",
        "pagination"
      ],
      "columns": [
        {
          "name": "id",
          "label": "ID",
          "hidden": true,
          "sortable": true,
          "type": "text",
          "toggled": true
        },
        {
          "name": "name",
          "label": "课程名称",
          "sortable": false,
          "searchable": false,
          "type": "text",
          "toggled": true,
          // "quickEdit": {
          //   "type": "text",
          //   "saveImmediately": true
          // },
          // "quickEditEnabledOn": "isInArray(this.front_end, 'admin_list_update')",
        },
        {
          "name": "type_map",
          "label": "课程类型",
          "sortable": false,
          "searchable": false,
          "type": "text",
          "toggled": true,
        },
        {
          "name": "category_name",
          "label": "课程分类",
          "sortable": false,
          "searchable": false,
          "type": "text",
          "toggled": true,
        },
        {
          "name": "amount",
          "label": "课程金额(元)",
          "sortable": false,
          "searchable": false,
          "type": "text",
          "toggled": true,
        },
        {
          "name": "expired_days",
          "label": "过期天数(天)",
          "sortable": false,
          "searchable": false,
          "type": "text",
          "toggled": true,
        },
        {
          "name": "state",
          "type": "switch",
          "width": 50,
          "label": "禁用/启用",
          "trueValue": 1,
          "falseValue": 2,
          "quickEdit": {
            "type": "switch",
            "trueValue": 1,
            "falseValue": 2,
            "mode": "inline",
            "saveImmediately": true,
            // "disabledOn": "!isInArray(this.front_end, 'admin_list_update') || this.is_super === 1"
          }
        },
        {
          "name": "creator",
          "label": "创建人",
          "type": "text",
          "sortable": false,
          "toggled": true
        },
        {
          "name": "created_at",
          "label": "创建时间",
          "type": "text",
          "sortable": false,
          "toggled": true
        },
        {
          "type": "operation",
          "label": "操作",
          "buttons": [
            {
              "type": "button",
              "label": "编辑",
              // "visibleOn": "isInArray(this.front_end, 'scale_category_update')",
              "level": "primary",
              "actionType": "drawer",
              "drawer": {
                "position": "right",
                "size": "lg",
                "title": "编辑",
                "closeOnOutside": true,
                "closeOnEsc": true,
                "resizable": true,
                "body": {
                  "type": "form",
                  // "debug": true,
                  "name": "编辑",
                  "api": "post:/b/course/update?id=$id",
                  "controls": getFormBody('2')
                }
              }
            },
            {
              "type": "action",
              "label": "设置章",
              // "visibleOn": "isInArray(this.front_end, 'company_list_contract')",
              "visibleOn": "this.type === 2",
              "level": "primary",
              "actionType": "drawer",
              "drawer": {
                "title": "设置章",
                "closeOnOutside": true,
                "closeOnEsc": true,
                "resizable": true,
                "position": "right",
                "size": "xl",
                "close": false,
                "actions": [
                  {
                    "type": "button",
                    "actionType": "close",
                    "label": "关闭",
                    "primary": false
                  }
                ],
                "data": {
                  "course_id": "${id}",
                },
                "body": {
                  "type": "crud",
                  "hideQuickSaveBtn": true,
                  "api": "get:/b/chapter/index",
                  "quickSaveApi": "post:/b/chapter/batch",
                  "quickSaveItemApi": "post:/b/chapter/item?id=$id",
                  "perPage": 10,
                  "keepItemSelectionOnPageChange": true,
                  "maxKeepItemSelectionLength": 20,
                  "pageField": "page",
                  "perPageField": "page_size",
                  "labelTpl": "${name}",
                  "syncLocation": false,
                  "filter": {
                    "title": "条件搜索",
                    // "debug": true,
                    "submitText": "搜索",
                    "controls": [
                      {
                        "type": "hidden",
                        "name": "course_id"
                      },
                      {
                        "label": "章名称：",
                        "type": "text",
                        "size": "sm",
                        "name": "name",
                        "placeholder": "请输入"
                      },
                    ],
                    "actions": [
                      {
                        "type": "button",
                        "actionType": "clear",
                        "label": "重置"
                      },
                      {
                        "type": "button",
                        "actionType": "submit",
                        "label": "搜索",
                        "level": "primary",
                      }
                    ]
                  },
                  "bulkActions": [
                    // {
                    //   "label": "批量删除",
                    //   // "visibleOn": "this.front_end.includes('system_role_delete')",
                    //   "level": "danger",
                    //   "actionType": "ajax",
                    //   "api": "post:/b/contract/del/${ids|raw}",
                    //   "confirmText": "确定要批量删除?"
                    // }
                  ],
                  "filterTogglable": true,
                  "headerToolbar": [
                    "filter-toggler",
                    "bulkActions",
                    {
                      "type": "button",
                      "actionType": "drawer",
                      "label": "新增",
                      // "visibleOn": "this.front_end.includes('system_role_save')",
                      "icon": "fa fa-plus pull-left",
                      "primary": true,
                      "drawer": {
                        "title": "新增",
                        "closeOnOutside": true,
                        "closeOnEsc": true,
                        "resizable": true,
                        "position": "right",
                        "size": "lg",
                        "body": {
                          "type": "form",
                          // "debug": true,
                          "persistData": "course_chapter_insert",
                          "clearPersistDataAfterSubmit": true,
                          "mode": "horizontal",
                          "api": {
                            "method": "post",
                            'url': "/b/chapter/save",
                            "dataType": "form"
                          },
                          "controls": getChapterFormBody('1')
                        },
                      }
                    },
                    {
                      "type": "columns-toggler",
                      "align": "right"
                    },
                    {
                      "type": "pagination",
                      "align": "right"
                    }
                  ],
                  "footerToolbar": [
                    "statistics",
                    "switch-per-page",
                    "pagination"
                  ],
                  "columns": [
                    {
                      "name": "id",
                      "label": "ID",
                      "width": 50,
                      "sortable": true,
                      "hidden": true,
                      "type": "text",
                      "toggled": true
                    },
                    {
                      "name": "name",
                      "label": "章名称",
                      "sortable": false,
                      "searchable": true,
                      "type": "text",
                      "toggled": true,
                    },
                    {
                      "name": "creator",
                      "label": "创建人",
                      "type": "text",
                      "sortable": false,
                      "toggled": true
                    },
                    {
                      "name": "created_at",
                      "label": "创建时间",
                      "type": "text",
                      "sortable": true,
                      "toggled": true
                    },
                    {
                      "type": "operation",
                      "label": "操作",
                      "buttons": [
                        {
                          "type": "button",
                          "label": "编辑",
                          // "visibleOn": "this.front_end.includes('system_role_update')",
                          "level": "primary",
                          "actionType": "drawer",
                          "drawer": {
                            "position": "right",
                            "size": "lg",
                            "title": "编辑",
                            "closeOnOutside": true,
                            "closeOnEsc": true,
                            "resizable": true,
                            "body": {
                              "type": "form",
                              "api": "post:/b/chapter/update?id=$id",
                              "mode": "horizontal",
                              "controls": getChapterFormBody('2')
                            },
                          },
                        },
                        {
                          "type": "button",
                          "label": "删除",
                          // "visibleOn": "this.front_end.includes('system_role_delete')",
                          // "level": "danger",
                          "actionType": "ajax",
                          "confirmText": "您确认要删除?",
                          "api": "post:/b/chapter/delete?ids=$id"
                        }
                      ],
                      "toggled": true
                    }
                  ]
                }
              },
            },
            {
              "type": "action",
              "label": "设置节",
              // "visibleOn": "isInArray(this.front_end, 'company_list_contract')",
              "visibleOn": "this.type === 2",
              "level": "primary",
              "actionType": "drawer",
              "drawer": {
                "title": "设置节",
                "closeOnOutside": true,
                "closeOnEsc": true,
                "resizable": true,
                "position": "right",
                "size": "xl",
                "close": false,
                "actions": [
                  {
                    "type": "button",
                    "actionType": "close",
                    "label": "关闭",
                    "primary": false
                  }
                ],
                "data": {
                  "course_id": "${id}",
                },
                "body": {
                  "type": "crud",
                  "hideQuickSaveBtn": true,
                  "api": "get:/b/section/index",
                  "quickSaveApi": "post:/b/section/batch",
                  "quickSaveItemApi": "post:/b/section/item?id=$id",
                  "perPage": 10,
                  "keepItemSelectionOnPageChange": true,
                  "maxKeepItemSelectionLength": 20,
                  "pageField": "page",
                  "perPageField": "page_size",
                  "labelTpl": "${name}",
                  "syncLocation": false,
                  "filter": {
                    "title": "条件搜索",
                    // "debug": true,
                    "submitText": "搜索",
                    "controls": [
                      {
                        "type": "hidden",
                        "name": "course_id"
                      },
                      {
                        "label": "节名称：",
                        "type": "text",
                        "size": "sm",
                        "name": "name",
                        "placeholder": "请输入"
                      },
                      {
                        "label": "章",
                        "type": "select",
                        "size": "sm",
                        "name": "course_chapter_id",
                        "searchable": true,
                        "clearable": true,
                        "source": "get:/b/chapter/options?state=1&course_id=$course_id",
                      },
                    ],
                    "actions": [
                      {
                        "type": "button",
                        "actionType": "clear",
                        "label": "重置"
                      },
                      {
                        "type": "button",
                        "actionType": "submit",
                        "label": "搜索",
                        "level": "primary",
                      }
                    ]
                  },
                  "bulkActions": [
                    // {
                    //   "label": "批量删除",
                    //   // "visibleOn": "this.front_end.includes('system_role_delete')",
                    //   "level": "danger",
                    //   "actionType": "ajax",
                    //   "api": "post:/b/contract/del/${ids|raw}",
                    //   "confirmText": "确定要批量删除?"
                    // }
                  ],
                  "filterTogglable": true,
                  "headerToolbar": [
                    "filter-toggler",
                    "bulkActions",
                    {
                      "type": "button",
                      "actionType": "drawer",
                      "label": "新增",
                      // "visibleOn": "this.front_end.includes('system_role_save')",
                      "icon": "fa fa-plus pull-left",
                      "primary": true,
                      "drawer": {
                        "title": "新增",
                        "closeOnOutside": true,
                        "closeOnEsc": true,
                        "resizable": true,
                        "position": "right",
                        "size": "lg",
                        "body": {
                          "type": "form",
                          // "debug": true,
                          "persistData": "course_section_insert",
                          "clearPersistDataAfterSubmit": true,
                          "mode": "horizontal",
                          "api": {
                            "method": "post",
                            'url': "/b/section/save",
                            "dataType": "form"
                          },
                          "controls": getSectionFormBody('1')
                        },
                      }
                    },
                    {
                      "type": "columns-toggler",
                      "align": "right"
                    },
                    {
                      "type": "pagination",
                      "align": "right"
                    }
                  ],
                  "footerToolbar": [
                    "statistics",
                    "switch-per-page",
                    "pagination"
                  ],
                  "columns": [
                    {
                      "name": "id",
                      "label": "ID",
                      "width": 50,
                      "sortable": true,
                      "hidden": true,
                      "type": "text",
                      "toggled": true
                    },
                    {
                      "name": "name",
                      "label": "节名称",
                      "sortable": false,
                      "searchable": true,
                      "type": "text",
                      "toggled": true,
                    },
                    {
                      "name": "chapter_name",
                      "label": "章名称",
                      "sortable": false,
                      "searchable": true,
                      "type": "text",
                      "toggled": true,
                    },
                    {
                      "name": "creator",
                      "label": "创建人",
                      "type": "text",
                      "sortable": false,
                      "toggled": true
                    },
                    {
                      "name": "created_at",
                      "label": "创建时间",
                      "type": "text",
                      "sortable": true,
                      "toggled": true
                    },
                    {
                      "type": "operation",
                      "label": "操作",
                      "buttons": [
                        {
                          "type": "button",
                          "label": "编辑",
                          // "visibleOn": "this.front_end.includes('system_role_update')",
                          "level": "primary",
                          "actionType": "drawer",
                          "drawer": {
                            "position": "right",
                            "size": "lg",
                            "title": "编辑",
                            "closeOnOutside": true,
                            "closeOnEsc": true,
                            "resizable": true,
                            "body": {
                              "type": "form",
                              "api": "post:/b/section/update?id=$id",
                              "mode": "horizontal",
                              "controls": getSectionFormBody('2')
                            },
                          },
                        },
                        {
                          "type": "button",
                          "label": "删除",
                          // "visibleOn": "this.front_end.includes('system_role_delete')",
                          // "level": "danger",
                          "actionType": "ajax",
                          "confirmText": "您确认要删除?",
                          "api": "post:/b/section/delete?ids=$id"
                        }
                      ],
                      "toggled": true
                    }
                  ]
                }
              },
            },
          ],
          "toggled": true
        }
      ]
    }
  };
})();